<?php
 	include './init.php';
 	//echo $gid;
 	//根据商品id查找商品所有信息
	if(empty($_GET['gid'])){
		header("location:category.php");
	}else{
		$gid = $_GET['gid'];
	}
	//根据gid，查询商品
	$sql = "SELECT g.id,g.name,g.cate_id,g.price,g.stock,g.is_hot,g.is_new,g.is_best,g.`describe`,i.name as iname FROM ".PRE."goods g,".PRE."image i WHERE g.id={$gid} AND g.id=i.good_id AND i.is_face=1" ;
    $result = mysqli_query($link,$sql);
    if($result && mysqli_num_rows($result)>0){
        //var_dump($result);
        $row = mysqli_fetch_assoc($result);
        $name = $row['iname'];
        $url  = URL."uploads/";
        $url .= substr($name,0,4).'/';
        $url .= substr($name,4,2).'/';
        $url .= substr($name,6,2).'/';
        $url .=$name;
        $row['url']=$url;
	}

	//var_dump($row);


	//根据gid，查询商品小图片
	$sql = "SELECT name FROM ".PRE."image WHERE good_id={$gid} LIMIT 4" ;
	//echo $sql;
    $result = mysqli_query($link,$sql);
    if($result && mysqli_num_rows($result)>0){
        //var_dump($result);
        while($im = mysqli_fetch_assoc($result)){
	        $im = $im['name'];
	        $img  = URL."uploads/";
	        $img .= substr($im,0,4).'/';
	        $img .= substr($im,4,2).'/';
	        $img .= substr($im,6,2).'/';
	        $img .=$im;
	        $pic['img']=$img;
	        $smallpic[] = $pic;
    	}
	}

	//var_dump($smallpic);
	//var_dump($_GET['num']);
	if(empty($_GET['num'])){
		$amo = 1;
	}else{
		if($_GET['num']==1){
			$amo = $_GET['amo']+1;
		}else{
			$amo = $_GET['amo']-1;
			$amo = $amo?$amo:'1';
		}
	}
?>

<!DOCTYPE html>
<html>
<head>
	<title>商品详情页</title>
	<meta charset="utf-8">
	<link rel="shortcut icon" href="./admin/images/favicon.ico" />
	<link rel="stylesheet" type="text/css" href="./css/public.css">
	<link rel="stylesheet" type="text/css" href="./css/head.css">
	<link rel="stylesheet" type="text/css" href="./css/foot.css">
	<link rel="stylesheet" type="text/css" href="./css/detail.css">
</head>
<body>
	<div>
		<?php
		//加载网页头
		include './index_head.php';
		?>
		<div id="main_body">
			<?php //展示卖家信息条?>
			<div class="wd seller">
				<img class="fr" src="./image/searchshop.png">
			</div>
			<?php //头部卖家导航条?>
			<div class="seller_nav">
				<div class="nav">
					<ul class="wd">
						<li><a href="">首页</a></li>
						<li><a href="">全部商品</a></li>
						<li><a href="">最新上架</a></li>
						<li><a href="">热门卖品</a></li>
						<li><a href="">全店最精</a></li>
					</ul>
				</div>
			</div>
			<?php //详情区域?>
			<div class="wd detail_wrap">
				<!--图片展示-->
				<div class="picshow fl">
					<img src="<?php echo $row['url'];?>">
					<ul>
						<?php foreach($smallpic as $p){?>
						<li class="fl"><a href=""><img src="<?php echo $p['img'];?>"></a></li>
						<?php }?>
					</ul>

				</div>
				<!--详细信息-->
				<div class="detail fl">
					<div class="gname"><?php echo $row['describe'];?></div>
					<div class="price">
						<span>原价：￥<?php echo $row['price'];?></span>
						<span>现价：￥<?php echo $row['price'];?></span>
						<span>店铺优惠：无</span>
					</div>
					<div class="cserver">
						<p>客服:</p><a href=""><img src="./image/cserver.gif"></a>
					</div>
					<div class="cl"></div>
					<div class="color">颜色:无</div>
					<div class="amount">
						<p>数量:</p><a href="detail.php?num=2&gid=<?php echo $gid?>&amo=<?php echo $amo;?>">-</a><input type="text" value="<?php echo $amo<=$row['stock']?$amo:$row['stock'];?>"><a href="detail.php?num=1&gid=<?php echo $gid?>&amo=<?php echo $amo;?>">+</a><p>&nbsp;&nbsp;&nbsp;库存:<?php echo $row['stock'];?></p>
					</div>
					<div class="buy">
						<a class="buynow" href="action.php?a=cart&gid=<?php echo $gid;?>&amo=<?php echo $amo;?>">立即购买</a>
						<a class="shopcar" href="action.php?a=cart&gid=<?php echo $gid;?>&amo=<?php echo $amo;?>">加入购物车</a>
						<a class="like" href="">&nbsp;&nbsp;&nbsp;收藏</a>
						<a class="share" href="">&nbsp;&nbsp;&nbsp;分享</a>
						<a class="report" href="">举报</a>
					</div>
					<div class="sword">
						服务承诺:<img src="./image/sword.png">
					</div>
					<div class="payway">
						支付方式:<div class="paywaypic"></div>
					</div>
				</div>
				<!--推荐侧栏-->
				<div class="recommend fr">
					暂无推荐
				</div>
				<div class="cl"></div>
			</div>
			<?php //商品评论区域?>
			<div class="wd comment">
				<?php
				if(!empty($_GET['oid'])){
				$oid = $_GET['oid'];
				
        		//查询订单状态
				$sql = "SELECT status FROM ".PRE."order WHERE id={$oid}";
				$res = mysqli_query($link,$sql);
				$ostatus = mysqli_fetch_assoc($res)['status'];
				if($ostatus==4){
					//修改订单状态为已评价
					$sql = "UPDATE ".PRE."order SET status=5 WHERE id={$oid}";
					//echo $sql;
        			$res = mysqli_query($link,$sql);
					?>
				<div class="fr wrap">
					<div class="head">请对你购买的商品进行评价</div>
					<form action="action.php?a=comment" method="post" enctype="multipart/form-data">
						<div class="row">
							<input type="hidden" name="gid" value="<?php echo $_GET['gid'];?>">
							<div class="l fl">评价：</div>
							<div class="r fl"><textarea name="cword" cols="80" rows="3"></textarea></div>	
						</div>
						<div class="cl"></div>
						<div class="row">
							<div class="l fl">上传图片：</div>
							<div class="r fl"><input type="file" name="pic" value=""></div>
							<div class="fr sub"><input type="submit" value="确认评价"></div>
						</div>
						<div class="cl"></div>
					</form>
				</div>
				<div class="cl"></div>
				<?php }}?>
				<?php //加载其他人的评论
					$sql = "SELECT id,message,pic,uid,`time` FROM ".PRE."comment WHERE gid={$gid}";
					$res = mysqli_query($link,$sql);
					if($res && mysqli_num_rows($res)){
						while ($coms = mysqli_fetch_assoc($res)){
							$comment[] = $coms;
						}
					}
					if(!empty($comment)){
						//var_dump($comment);
						foreach ($comment as $v) {
				?>
				<div class="fr wrap">
					<div class="head">用户id-<?php echo $v['uid']?>，<?php echo date('Y-m-d H:i:s',$v['time'])?></div>
					<div class="row">
						<div class="l fl">评价内容：</div>
						<div class="content fl"><?php echo $v['message']?></div>	
					</div>
					<div class="cl"></div>
					<div class="row">
						<div class="l fl">图片show：</div>
						<div class="r fl"><img width="70" height="70" alt="无法加载图片"src="<?php
						$url = URL.'uploads/comment/';
						$url .= substr($v['pic'],0,4).'/';
						$url .= substr($v['pic'],4,2).'/';
						$url .= substr($v['pic'],6,2).'/';
						echo $url.$v['pic']
						?>"></div>
					</div>
					<div class="cl"></div>
				</div>
				<div class="cl"></div>
				<?php }}else{?>
				<div class="fr wrap">暂无评论</div>
				<div class="cl"></div>
				<?php }?>
			</div>
		</div>
		<?php
		//加载网页脚
		include './index_foot.php';
		?>
	</div>
</body>
</html>